package com.zr.plant.service.impl;


import com.zr.plant.mapper.PlantMapper;
import com.zr.plant.pojo.*;
import com.zr.plant.service.PlantService;
import com.zr.utils.AllRecords;
import com.zr.utils.ResultBuilderVo;
import com.zr.utils.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * Created by Lenovo on 2019/12/25.
 */
@Service
public class PlantServiceImpl implements PlantService{
    @Autowired
    private PlantMapper plantMapper;
    @Override
    public ResultVo queryLegalPersonById(Integer userId) {
        List<LegalPersonVo> legalPersonVoList =  plantMapper.queryLegalPersonById(userId);
        return ResultBuilderVo.success(legalPersonVoList);
    }

    @Override
    public ResultVo queryPage(PlantSelectVo plantSelectVo) {
        AllRecords allRecords = new AllRecords();
        //根据条件查询总条数
        int count =  plantMapper.queryCount(plantSelectVo);
        //根据条件查询数据
        List<PlantVo> planClassVoList = plantMapper.queryData(plantSelectVo);
      /*  for (PlantVo planClassVo:planClassVoList){
            if (planClassVo.getStatus()!=null){
                if (!planClassVo.getStatus()){
                    planClassVo.setStatusName("禁用");
                }else {
                    planClassVo.setStatusName("启用");
                }
            }

        }*/
        allRecords.setPageIndex(plantSelectVo.getPageIndex());
        allRecords.setPageSize(plantSelectVo.getPageSize());
        allRecords.setTotalNumber(count);
        allRecords.setDataList(planClassVoList);
        allRecords.resetTotalNumber(count);
        return ResultBuilderVo.success(allRecords);
    }

    @Override
    @Transactional
    public ResultVo add(@Valid PlantAddVo plantAddVo) {

      ResultVo resultVo = validate(plantAddVo);
      if (!resultVo.getSuccess()){
          return resultVo;
      }
      //数据赋值
        PlantVo plantVo = new PlantVo();
        BeanUtils.copyProperties(plantAddVo,plantVo);
        Date nowDate = new Date();
        plantVo.setCreateName("小明");
        plantVo.setCreateTime(nowDate);
        plantVo.setUpdateName("小明");
        plantVo.setUpdateTime(nowDate);
        //入库
        plantMapper.insertPlant(plantVo);
        return ResultBuilderVo.success();
    }

    @Override
    @Transactional
    public ResultVo updte(@Valid PlantUpdateVo plantUpdateVo) {
        //根据code查询工厂信息
      PlantVo plantVo = plantMapper.queryPlantByCode(plantUpdateVo.getLegalPlantCode());
      if (plantVo==null){
          return ResultBuilderVo.error("修改信息不村子!");
      }
      //验证数据状态是否为禁用状态
      if (plantVo.getEnabled()){
          return ResultBuilderVo.error("禁用状态属于才能修改!");
      }
      //数据赋值
        PlantVo plantVo1 = new PlantVo();
        BeanUtils.copyProperties(plantUpdateVo,plantVo1);
        plantVo.setUpdateName("小明");
        plantVo.setUpdateTime(new Date());
        plantMapper.updatePlant(plantVo1);
        return ResultBuilderVo.success();
    }

    @Override
    @Transactional
    public ResultVo updateStatus(@Valid PlantUpdateStatusVo plantUpdateStatusVo) {
        PlantVo plantVo = plantMapper.queryPlantById(plantUpdateStatusVo.getId());
        if (plantVo==null){
            return ResultBuilderVo.error("修改信息不村子!");
        }
        //数据赋值
        PlantVo plantVo1 = new PlantVo();
        BeanUtils.copyProperties(plantUpdateStatusVo,plantVo1);
        plantVo.setUpdateName("小明");
        plantVo.setUpdateTime(new Date());
        plantMapper.updateSttusPlant(plantVo1);
        return ResultBuilderVo.success();
    }

    public ResultVo validate(PlantAddVo plantAddVo){
        //验证法人是否存在，
       int count =  plantMapper.queryLegalPersonByCode(plantAddVo.getLegalPersonCode());
       if (count==0){
           return ResultBuilderVo.error("请验证法人信息，法人信息不存在!");
       }
        // 验证工厂是否重复即可
        PlantVo plantCount =  plantMapper.queryPlantByCode(plantAddVo.getLegalPlantCode());
        if (plantCount!=null){
            return ResultBuilderVo.error("工厂已经存在!");
        }
        return ResultBuilderVo.success();
    }
}
